﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using PominaScale.Properties;
using PominaScale.DataAccess;
using PominaScale.Reports;
using System.IO;

namespace PominaScale
{
    public partial class FrmMDI : Form
    {
        #region Member variables
        public Timer _timer;
        #endregion

        #region Singleton Members
        private static volatile FrmMDI instance;
        private static object syncRoot = new Object();

        public static FrmMDI Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmMDI();
                }

                return instance;
            }
        }

        #endregion

        #region Constructors
        private FrmMDI()
        {
            InitializeComponent();

            Initialize();
        }
        #endregion

        #region Methods
        public void Initialize()
        {
            // System
            this.ConfigMenuItem.Click += new EventHandler(MenuItem_Click);
            this.UserMenuItem.Click += new EventHandler(MenuItem_Click);
            this.LogoutMenuItem.Click += new EventHandler(MenuItem_Click);
            this.ChangePasswordMenuItem.Click += new EventHandler(MenuItem_Click);

            this.ExitMenuItem.Click += new EventHandler(MenuItem_Click);

            // PO
            this.PoMenuItem.Click += new EventHandler(MenuItem_Click);
            this.PoListMenuItem.Click += new EventHandler(MenuItem_Click);
            this.InventoryMenuItem.Click += new EventHandler(MenuItem_Click);

            // List
            this.VendorMenuItem.Click += new EventHandler(MenuItem_Click);
            this.ProductMenuItem.Click += new EventHandler(MenuItem_Click);

            // Report
            this.ReportDateMenuItem.Click += new EventHandler(MenuItem_Click);

            this.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height - 30);
            this.Location = new Point(0, 0);

            // Set timer
            _timer = new Timer();
            _timer.Tick += new EventHandler(delegate
            {
                labelCommand.Text = DateTime.Now.ToString();
            });
            _timer.Start();
        }

        protected override void OnClosed(EventArgs e)
        {
            _timer.Stop();
            _timer.Dispose();

            base.OnClosed(e);
        }

        private void MenuItem_Click(object sender, EventArgs e)
        {
            switch ((sender as ToolStripMenuItem).Name)
            {
                case "ConfigMenuItem":
                    FrmConfiguration.Instance.MdiParent = this;
                    FrmConfiguration.Instance.Show();
                    break;
                case "UserMenuItem":
                    FrmUsers.Instance.MdiParent = this;
                    FrmUsers.Instance.Show();
                    break;
                case "LogoutMenuItem":
                    foreach (var f in this.MdiChildren.Where(c => !c.IsDisposed))
                    {
                        f.Close();
                    }
                    this.OnLoad(null);
                    break;
                case "ChangePasswordMenuItem":
                    FrmChangePassword.Instance.MdiParent = this;
                    FrmChangePassword.Instance.Show();
                    break;
                case "PoMenuItem":
                    FrmPO frmPo = new FrmPO();
                    frmPo.MdiParent = this;
                    frmPo.Show();
                    break;
                case "PoListMenuItem":
                    FrmPOList.Instance.MdiParent = this;
                    FrmPOList.Instance.Show();
                    break;
                case "InventoryMenuItem":
                    FrmWarehouse frmWareHouse = new FrmWarehouse();
                    frmWareHouse.MdiParent = this;
                    frmWareHouse.Show();
                    break;
                case "VendorMenuItem":
                    FrmVendor.Instance.MdiParent = this;
                    FrmVendor.Instance.Show();
                    break;
                case "ProductMenuItem":
                    FrmProducts.Instance.MdiParent = this;
                    FrmProducts.Instance.Show();
                    break;
                case "ReportDateMenuItem":
                    FrmViewReport frmReport = new FrmViewReport();
                    frmReport.MdiParent = this;
                    frmReport.Show();
                    break;
                case "ExitMenuItem":
                    Application.Exit();
                    break;
            }
        }

        #endregion

        #region Events

        protected override void OnLoad(EventArgs e)
        {
            // Correct connection
            FrmLogin login = new FrmLogin();
            if (login.ShowDialog() != DialogResult.OK)
            {
                this.Close();
                return;
            }

            this.labelUserLogin.Text = String.Format(Resources.AccountLogin, Common.UserLogin.Username);

            // Create the directories to store the scale data
            CreateMultiLevelDirectory(Settings.Default.MM_S2C);
            CreateMultiLevelDirectory(Settings.Default.MM_C2S);
            CreateMultiLevelDirectory(Settings.Default.SD_S2C);
            CreateMultiLevelDirectory(Settings.Default.SD_C2S);
            CreateMultiLevelDirectory(Settings.Default.Stored + "MM_S2C");
            CreateMultiLevelDirectory(Settings.Default.Stored + "SD_S2C");
            CreateMultiLevelDirectory(Settings.Default.Stored + "MM_C2S");
            CreateMultiLevelDirectory(Settings.Default.Stored + "SD_C2S");

            // Set permission
            bool IsAdmin = (Common.UserLogin.GroupID == 1);
            UserMenuItem.Visible = IsAdmin;
            ListMenu.Visible = IsAdmin;

            base.OnLoad(e);
        }

        private void CreateMultiLevelDirectory(string path)
        {
            while (path.Contains('\\'))
            {
                // Convert to the same format
                path = path.Replace('\\', '/');
            }

            string[] paths = path.Split('/');
            int length = paths.Length;
            if (length > 0)
            {
                int i = 0;

                // Format C:/Source/Template ...
                if (paths[0].EndsWith(":"))
                {
                    string drive = paths[0];
                    path = drive + '/';
                    i++;
                }
                else
                {
                    // Server path
                    path = String.Empty;
                }

                for (i = 1; i < length; i++)
                {
                    path = String.Concat(path, paths[i], '/');
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                }
            }
            else
            {
                throw new InvalidExpressionException("Invalid path.");
            }
        }

        #endregion

    }
}
